package com.wltt.product.frfront.config.robot;

import com.wltt.product.frfront.config.exception.RobotException;
import com.wltt.product.frfront.holder.RobotHolder;
import com.wltt.product.frfront.mapper.RobotLogMapper;
import com.wltt.product.frfront.mapper.RobotMapper;
import com.wltt.product.frfront.model.po.RobotLogPO;
import com.wltt.product.frfront.model.po.RobotPO;
import fairino.DescPose;
import fairino.ExaxisPos;
import fairino.JointPos;
import fairino.Robot;
import org.springframework.http.HttpStatus;

import java.util.Date;

public class WlttRobot extends Robot {

//    private RobotLogMapper robotLogMapper;

    private RobotMapper robotMapper;

    private RobotPO robotPO;

    public WlttRobot(RobotMapper robotMapper, RobotLogMapper robotLogMapper, RobotPO robotPO) {
        super();
        this.robotMapper = robotMapper;
//        this.robotLogMapper = robotLogMapper;
        this.robotPO = robotPO;
    }

    public RobotPO getRobotPO() {
        return robotPO;
    }

    public void setRobotPO(RobotPO robotPO) {
        this.robotPO = robotPO;
    }

    public void save() {
        robotMapper.updateById(robotPO);
    }

    public boolean connectToRobot(String ip) {
        int rtn =  RobotHolder.getRobot().RPC(ip);
        if (rtn != 0) {
            return false;
        } else {
            RobotHolder.setRobotIp(ip);
            RobotHolder.getRobot().ResetAllError();
            RobotHolder.setIsError(false);
            return true;
        }
    }
    public int connectToRobot() {
        return RobotHolder.getRobot().RPC(RobotHolder.getRobotIp());
    }

    public boolean isConnected() {
        return super.isConnected();
    }

    public int GetInverseKin(int type, DescPose desc_pos, int config, JointPos joint_pos) {
        int result = super.GetInverseKin(type, desc_pos, config, joint_pos);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int GetActualJointPosDegree(JointPos jPos) {
        int result = super.GetActualJointPosDegree(jPos);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int GetActualTCPPose(DescPose desc_pos) {
        int result = super.GetActualTCPPose(desc_pos);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int StartJOG(int refType, int nb, int dir, double vel, double acc, double max_dis) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("StartJOG");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.StartJOG(refType, nb, dir, vel, acc, max_dis);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int ImmStopJOG() {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("ImmStopJOG");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.ImmStopJOG();
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int SetForwardWireFeed(int ioType, int wireFeed) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("SetForwardWireFeed");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.SetForwardWireFeed(ioType, wireFeed);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int SetReverseWireFeed(int ioType, int wireFeed) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("SetReverseWireFeed");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.SetReverseWireFeed(ioType, wireFeed);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int SetAspirated(int ioType, int airControl) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("SetAspirated");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.SetAspirated(ioType, airControl);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int ResetAllError() {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("ResetAllError");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.ResetAllError();
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int GetRobotErrorCode(int[] maincode, int[] subcode) {
        int result = super.GetRobotErrorCode(maincode, subcode);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int MoveJ(JointPos joint_pos, DescPose desc_pos, int tool, int user, double vel, double acc, double ovl, ExaxisPos epos, double blendT, int offset_flag, DescPose offset_pos) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("MoveJ");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.MoveJ(joint_pos, desc_pos, robotPO.getToolCoordinateNum(), robotPO.getWorkpieceCoordinateNum(), vel, acc, ovl, epos, blendT, offset_flag, offset_pos);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int MoveL(JointPos joint_pos, DescPose desc_pos, int tool, int user, double vel, double acc, double ovl, double blendR, ExaxisPos epos, int search, int offset_flag, DescPose offset_pos, int overSpeedStrategy, int speedPercent) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("MoveL");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.MoveL(joint_pos, desc_pos, robotPO.getToolCoordinateNum(), robotPO.getWorkpieceCoordinateNum(), vel, acc, ovl, blendR, epos, search, offset_flag, offset_pos, overSpeedStrategy, speedPercent);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }


    public int MoveC(JointPos joint_pos_p, DescPose desc_pos_p, int ptool, int puser, double pvel, double pacc, ExaxisPos epos_p, int poffset_flag, DescPose offset_pos_p, JointPos joint_pos_t, DescPose desc_pos_t, int ttool, int tuser, double tvel, double tacc, ExaxisPos epos_t, int toffset_flag, DescPose offset_pos_t, double ovl, double blendR) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("MoveC");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.MoveC(joint_pos_p, desc_pos_p, robotPO.getToolCoordinateNum(), robotPO.getWorkpieceCoordinateNum(), pvel, pacc, epos_p, poffset_flag, offset_pos_p, joint_pos_t, desc_pos_t, robotPO.getToolCoordinateNum(), robotPO.getWorkpieceCoordinateNum(), tvel, tacc, epos_t, toffset_flag, offset_pos_t, ovl, blendR);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int WeaveSetPara(int weaveNum, int weaveType, double weaveFrequency, int weaveIncStayTime, double weaveRange, double weaveLeftRange, double weaveRightRange, int additionalStayTime, int weaveLeftStayTime, int weaveRightStayTime, int weaveCircleRadio, int weaveStationary, double weaveYawAngle, double weaveRotAngle) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("WeaveSetPara");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.WeaveSetPara(weaveNum, weaveType, weaveFrequency, weaveIncStayTime, weaveRange, weaveLeftRange, weaveRightRange, additionalStayTime, weaveLeftStayTime, weaveRightStayTime, weaveCircleRadio, weaveStationary, weaveYawAngle, weaveRotAngle);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int WeaveStart(int weaveNum) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("WeaveStart");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.WeaveStart(weaveNum);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int WeaveEnd(int weaveNum) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("WeaveEnd");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.WeaveEnd(weaveNum);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int WeldingSetCurrent(int ioType, double current, int AOIndex, int blend) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("WeldingSetCurrent");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.WeldingSetCurrent(ioType, current, AOIndex, blend);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int WeldingSetVoltage(int ioType, double voltage, int AOIndex, int blend) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("WeldingSetVoltage");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.WeldingSetVoltage(ioType, voltage, AOIndex, blend);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int ARCStart(int ioType, int arcNum, int timeout) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("ARCStart");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.ARCStart(ioType, arcNum, timeout);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int ARCEnd(int ioType, int arcNum, int timeout) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("ARCEnd");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.ARCEnd(ioType, arcNum, timeout);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int StopMotion() {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("StopMotion");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.StopMotion();
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

    public int Mode(int mode) {
//        RobotLogPO robotLogPO = new RobotLogPO();
//        robotLogPO.setCommandInfo("Mode");
//        robotLogPO.setCommandSendTime(new Date());
        int result = super.Mode(mode);
//        robotLogPO.setCommandExecuteResult(String.valueOf(result));
//        robotLogMapper.insertSelective(robotLogPO);
        if (result != 0) {
            throw new RobotException(RobotHolder.getErrorMessage(result), HttpStatus.BAD_REQUEST);
        }
        return result;
    }

}
